<template>
  <div class="loading-wrap">
    <div class="mask">
      <logo class="logo" />
    </div>
    <div class="content">
      <img class="earth" src="@/assets/png/earth.png" alt="" />
      <div class="icon-wrap">
        <div class="progress">
          <div class="progress-inner" :style="{ width: `${floor(percent)}%` }"></div>
        </div>
        <img ref="shipRef" class="ship" src="@/assets/png/icon5.png" alt="" />
      </div>
      <div class="text-wrap">正在努力加载场景，请稍候……</div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { floor } from 'lodash';
import logo from '@/components/logo.vue';

defineOptions({
  name: 'Loading',
});
interface IProps {
  loadingComplete: boolean;
}

const props = withDefaults(defineProps<IProps>(), {
  loadingComplete: false,
});

const emits = defineEmits(['close']);

const percent = ref<number>(0);
const shipRef = ref();
let worker = null as any;

onMounted(() => {
  worker = new Worker('/cimup/lib/loading.js');
  worker.onmessage = (e: any) => {
    const result = e.data;
    if (result.name === 'percent') {
      if (props.loadingComplete) {
        percent.value = 100;
        return;
      }
      percent.value = result.value;
      shipRef.value.style!.left = `calc(${percent.value}% - 13px)`;
      return;
    }
    if (result.name === 'close' && props.loadingComplete) {
      worker.terminate();
      worker = null;
      setTimeout(() => {
        emits('close');
      }, 500);
      return;
    }
    if (result.name === 'forceClose') {
      worker.terminate();
      worker = null;
      setTimeout(() => {
        emits('close');
      }, 500);
    }
  };
});

watch(
  () => props.loadingComplete,
  () => {
    if (worker) {
      percent.value = 100;
      shipRef.value.style.left = `calc(100% - 13px)`;
      worker.terminate();
      worker = null;
      setTimeout(() => {
        emits('close');
      }, 500);
    }
  },
  { deep: true }
);
</script>

<style lang="less" scoped>
.loading-wrap {
  display: flex;
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
  justify-content: center;
  align-items: center;
  background: #000;
  z-index: 100;
  .mask {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background: url('~@/assets/png/loading-bg.png') no-repeat;
    background-size: cover;

    .logo {
      position: absolute;
      top: 40px;
      left: 40px;
      width: 30px;
    }
  }
  .content {
    display: flex;
    flex-direction: column;
    align-items: center;
    position: relative;
    width: 520px;

    .earth {
      width: 240px;
      height: 240px;
      margin-bottom: 43px;
    }
    .icon-wrap {
      position: relative;
      .progress {
        position: relative;
        padding: 3px;
        width: 520px;
        height: 16px;
        background: #373839;
        box-shadow: inset 0px 8px 7px 0px rgba(0, 0, 0, 0.61);
        border-radius: 12px;
        border: 1px solid rgba(174, 175, 177, 0.6);
        z-index: 1;
        &-inner {
          height: 100%;
          background: linear-gradient(90deg, #0560fd 0%, #1187da 37%, #20e8f8 100%);
          border-radius: 8px;
        }
      }
      .ship {
        position: absolute;
        top: -10px;
        left: 0;
        z-index: 1;
      }
    }
    .text-wrap {
      font-size: 16px;
      font-weight: 400;
      color: fade(#ffffff, 60%);
      line-height: 22px;
      letter-spacing: 1px;
      padding-top: 15px;
    }
  }
}
</style>
